<?php
include('connect_db.php');
$err = ' ';

/*if logged in, redirect to homepage */
if (!checkLoggedin())
{
	header('Location: login.php');
}

$house_id = 0;
if(ISSET($_GET['house_id']))
{
	$house_id = $_GET['house_id'];
}
else if(ISSET($_POST['house_id']))
{
	$house_id = $_POST['house_id'];
}

if($house_id == 0)
{
	header('Location: index.php');//not legal access
}

$query = "SELECT * FROM houses_users
			WHERE houses_users.house_id = " . $house_id
	        ." AND houses_users.user_id = " . $_SESSION['id'];
$row = mysql_fetch_array(mysql_query($query));

$q = "SELECT * FROM houses WHERE id = ".$house_id;
$house = mysql_fetch_array(mysql_query($q));

$form_completed = true;
if(ISSET($_POST) && ISSET($_POST['amount']) 
     && ($_POST['amount'] == NULL || $_POST['category_id'] == NULL || 
     	 $_POST['notes'] == NULL || !is_numeric($_POST['amount'])))
{
	$err = 'Please complete all fields correctly.';
	$form_completed = false;
}

if(ISSET($row['privilege']))//below to this house
{
	if(ISSET($_POST) && ISSET($_POST['house_id']) && $form_completed)/* if posted */
	{
		$query = "INSERT INTO bills (house_id, user_id, amount, category_id, notes) VALUES
		(".$_POST['house_id'].", "
		  .$_SESSION['id'].", "
		  .$_POST['amount'].", "
		  .$_POST['category_id'].", "
		  ."'".$_POST['notes']."')";

		mysql_query($query);

		$bill_id = mysql_insert_id();
		for ($i = 0; $i < count($_POST['checked_fields']); $i++)
		{
			$query = "INSERT INTO bills_users (bill_id, user_id, shared_scale, if_paid) VALUES
					 	(".$bill_id.", "
					 	  .$_POST['checked_fields'][$i].", "
					 	  .floatval(1.0/(count($_POST['checked_fields'])+1.0))
					 	  .", 0)";
			mysql_query($query);
		}
		header('Location: mybills.php');
	}
}
else
{
	header('Location: index.php');//a user not belong to this house
}

?>
<script>
	function Refresh(id)
	{
		location.href="addbill.php?house_id=" + id;
	}
</script>

<?php include('templates/main/header.php'); ?>

    <div id="body">
		<div id="content" class="right_sidebar">
		<div id="addbill">
		<hr>
		<h1>Add Bill to 
			<select id="houses-form" name="houses" onChange="Refresh(this.value)">
				<?php
					$query = "SELECT * FROM houses
								JOIN houses_users ON houses.id = houses_users.house_id
								WHERE houses_users.user_id = " . $_SESSION['id'];
					$results = mysql_query($query);
					while($row = mysql_fetch_array($results))
					{
						echo '<option value="'.$row['house_id'].'" ';
						echo $house['id'] == $row['house_id'] ? ' selected="selected"' : '';
						echo ' >'.$row['name']. '</option>';
					}
				?>
			</select>
		</h1>
		<hr>
		<br />
		<br />
		
		<form action="addbill.php" method="post" accept-charset="utf-8">

		<?php if ($err != ' ') { ?>
		<div class="errors">
			<p><?php echo $err; ?></p>
		</div>
		<?php } ?>
		<div class="errors clear"></div>

		<h2>House Members</h2>
		<ul>
			<?php
		       	$query = "SELECT * FROM houses_users
		        JOIN users ON users.id = houses_users.user_id
		        WHERE houses_users.house_id = " . $house_id;

		        $results = mysql_query($query);
		        while($row = mysql_fetch_array($results))
		        {
		        	if($row['user_id'] != $_SESSION['id'])//do not show user himself
		        	{
		        		echo '<li><label><input name="checked_fields[]" value="'.$row['user_id'].'" type="checkbox" checked="checked">'
		        		 .$row['lastname'].' '.$row['firstname'].'</label></li>';
		        	}
		        }
			?>
		</ul>

		<label>
			Amount($)*:
			<input name="amount" value="" type="text">
		</label>

		<label>
			Reason:
			<textarea name="notes" cols="90" rows="7" value="" ></textarea>
		</label>

		<input type="hidden" name="house_id" value="<?php echo $house_id; ?>">

		<label>
			<div class="sameline">Category:</div>
			<select name="category_id">
			<option value="1">Rent</option>
			<option value="2">Electricity</option>
			<option value="3">Water</option>
			<option value="4">Gas</option>
			<option value="5">Cable</option>
			<option value="6">Food</option>
			<option value="7">House supplies</option>
			<option value="8">Entertainment</option>
			<option value="9">Repair</option>
			<option value="10">Transportation</option>
			<option value="11">Other</option>
			</select>
		</label>
		
		
		<input name="" value="Add" type="submit">
		</form>
	</div>
	</div>
	</div>

<?php include('templates/main/footer.php'); ?>